package com.beone.admin.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.beone.admin.exception.MvcException;
import com.beone.admin.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.beone.admin.common.AdminConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.beone.admin.entity.Feedback;
import com.beone.admin.service.FeedbackService;
import java.io.Serializable;

import org.springframework.stereotype.Controller;
import com.base.SuperController;


/**
 * @Title 意见反馈 前端控制器
 * @Author 秦波
 * @Version 1.0 on 2019-05-18
 * @Copyright 贝旺科权
 */
@Controller
@RequestMapping("/admin/feedback")
@Api(value = "意见反馈管理模块", tags = {"意见反馈管理接口"})
public class FeedbackController extends SuperController {

private static final Logger log = LoggerFactory.getLogger(FeedbackController.class);

@Autowired
private FeedbackService feedbackService;

    /**
     * 意见反馈管理 入口 --> index
     * @return
     */
    @GetMapping("")
    public String index() throws MvcException{
        return "feedback/index";
    }

    /**
     * 意见反馈管理 显示添加页面
     * @return
     */
    @GetMapping("/showAdd")
    public String showAdd()  throws MvcException{
        return "feedback/form";
    }


    /**
     * 意见反馈管理 显示修改页面
     * @return
     */
    @GetMapping("/showUpdate/{id}")
    public String showUpdate(@PathVariable("id") String id, ModelMap model) throws MvcException{
        try {
            model.addAttribute("id", id);
            model.addAttribute("entity", feedbackService.selectById(id));
        }catch (Exception e) {
            log.error("showUpdate 异常 e = {}", e);
            throw new MvcException(e.getMessage());
        }
        return "feedback/form";
    }

    /**
     * 意见反馈管理分页列表 JSON
     * @param entity
     * @param currPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "意见反馈列表", notes = "意见反馈列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true
                    , paramType = "param", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页最大显示记录数", required = true
                    , paramType = "param", dataType = "int")
    })
    @RequestMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE
            , method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object showList(Feedback entity,
    @RequestParam(value = "page", defaultValue = AdminConstants.DEFAULT_PAGE_NUM) int currPage,
    @RequestParam(value = "rows", defaultValue = AdminConstants.PAGE_SIZE) int pageSize) {
            return  feedbackService.getFeedbackPagination(entity, currPage, pageSize);
    }

    /**
     *  保存意见反馈
     * @param entity
     * @return
     */
    @ApiOperation(value = "保存意见反馈", notes = "保存意见反馈 主键属性为空新增,否则修改")
    @ApiImplicitParam(name = "entity", value = "{title}详细实体", required = true
            , paramType = "form", dataType = "object")
    @Log(desc = "意见反馈", type = Log.LOG_TYPE.SAVE)
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Object save(Feedback entity) {
        if(feedbackService.insertOrUpdate(entity)){
            return super.responseResult("success", "成功！");
        }
        return super.responseResult("fail", "失败！");
    }

;
    /**
     * 删除意见反馈
     * @param id
     * @return
     */
    @ApiOperation(value = "删除意见反馈", notes = "根据意见反馈Id，删除意见反馈")
    @ApiImplicitParam(name = "id", value = "实体ID", required = true, paramType = "param", dataType = "object")
    @Log(desc = "删除意见反馈", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id) {
        if(feedbackService.deleteById(id)){
            return super.responseResult("success", "成功！");
        }
        return super.responseResult("fail", "失败！");
    }
}
